package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.LineAdapter;
import flc.ast.bean.MyLineBean;
import flc.ast.databinding.ActivitySelLineBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

public class SelLineActivity extends BaseAc<ActivitySelLineBinding> {

    public static int selPosition;
    private List<MyLineBean> listLine = new ArrayList<>();
    private LineAdapter lineAdapter;
    private String key;

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_line;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivSelLineBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivSearch.setOnClickListener(this);

        mDataBinding.rvLineList.setLayoutManager(new LinearLayoutManager(mContext));
        lineAdapter = new LineAdapter();
        mDataBinding.rvLineList.setAdapter(lineAdapter);
        lineAdapter.setOnItemClickListener(this);

        mDataBinding.etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                key = mDataBinding.etSearch.getText().toString().trim();
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //先隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    if (key.isEmpty()) {
                        ToastUtils.showShort(getString(R.string.please_input_search));
                        return true;
                    }
                    List<MyLineBean> list = new ArrayList<>();
                    for (MyLineBean myLineBean : listLine) {
                        if (myLineBean.getName().contains(key)) {
                            list.add(myLineBean);
                        }
                    }
                    lineAdapter.setList(list);
                }
                return false;
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSearch://搜索
                key = mDataBinding.etSearch.getText().toString().trim();
                if (key.isEmpty()) {
                    ToastUtils.showShort(getString(R.string.please_input_search));
                    return;
                }
                List<MyLineBean> list = new ArrayList<>();
                for (MyLineBean myLineBean : listLine) {
                    if (myLineBean.getName().contains(key)) {
                        list.add(myLineBean);
                    }
                }
                lineAdapter.setList(list);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        lineAdapter.getItem(selPosition).setSelected(false);
        lineAdapter.notifyItemChanged(selPosition);
        selPosition = position;
        lineAdapter.getItem(position).setSelected(true);
        lineAdapter.notifyItemChanged(position);
        SPUtil.putInt(mContext, Content.Line, selPosition);
    }

    @Override
    protected void initData() {
        listLine.add(new MyLineBean(R.drawable.zg, getString(R.string.line_name1), false));
        listLine.add(new MyLineBean(R.drawable.zg, getString(R.string.line_name2), false));
        listLine.add(new MyLineBean(R.drawable.zg, getString(R.string.line_name3), false));
        listLine.add(new MyLineBean(R.drawable.mg, getString(R.string.line_name4), false));
        listLine.add(new MyLineBean(R.drawable.mg, getString(R.string.line_name5), false));
        listLine.add(new MyLineBean(R.drawable.mg, getString(R.string.line_name6), false));
        listLine.add(new MyLineBean(R.drawable.rb, getString(R.string.line_name7), false));
        listLine.add(new MyLineBean(R.drawable.rb, getString(R.string.line_name8), false));
        listLine.add(new MyLineBean(R.drawable.rb, getString(R.string.line_name9), false));
        listLine.add(new MyLineBean(R.drawable.yg, getString(R.string.line_name10), false));
        listLine.add(new MyLineBean(R.drawable.yg, getString(R.string.line_name11), false));
        listLine.add(new MyLineBean(R.drawable.yg, getString(R.string.line_name12), false));
        listLine.add(new MyLineBean(R.drawable.els, getString(R.string.line_name13), false));
        listLine.add(new MyLineBean(R.drawable.els, getString(R.string.line_name14), false));
        listLine.add(new MyLineBean(R.drawable.els, getString(R.string.line_name15), false));
        listLine.add(new MyLineBean(R.drawable.hg, getString(R.string.line_name16), false));
        listLine.add(new MyLineBean(R.drawable.hg, getString(R.string.line_name17), false));
        listLine.add(new MyLineBean(R.drawable.hg, getString(R.string.line_name18), false));
        listLine.add(new MyLineBean(R.drawable.xg, getString(R.string.line_name19), false));
        listLine.add(new MyLineBean(R.drawable.xg, getString(R.string.line_name20), false));
        listLine.add(new MyLineBean(R.drawable.xg, getString(R.string.line_name21), false));
        listLine.get(selPosition).setSelected(true);
        lineAdapter.setList(listLine);
    }
}